import { _decorator, Component, director, ProgressBar, resources } from 'cc'
const { ccclass, property } = _decorator

import FaderManager from '../../Runtime/FaderManager'

@ccclass('LoadingManager')
export class LoadingManager extends Component {
  @property(ProgressBar)
  progressBar: ProgressBar = null
  onLoad() {
    // 预加载
    this.preload()
  }

  // 预加载
  preload() {
    // 预加载场景
    director.preloadScene('Start')

    // 预加载资源
    resources.preloadDir('texture/ctrl', (cur: number, total: number) => {
      console.log('[ preload ] >', cur, total)
      let progress = cur / total
      console.log('[ progress ] >', progress)
      this.progressBar.progress = progress
    }, async err => {
      if (err) {
        await new Promise(rs => {
          setTimeout(rs, 2000)
        })
        this.preload()
        return
      }

      director.loadScene('Start')
    },)
  }
}
